package eao;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entity.Location;

/**
 * Session Bean implementation class EaoLocation
 */
@Stateless(mappedName = "eaoLocation")
@LocalBean
public class EaoLocation {

	@PersistenceContext
	private EntityManager em;
    /**
     * Default constructor. 
     */
    public EaoLocation() {
        // TODO Auto-generated constructor stub
    }
    

    public Location find(int id) {
        return em.find(Location.class, id);
    }

    public List<Location> list() {
    	return em.createQuery("SELECT l FROM Location l", Location.class).getResultList();
    }

    public void create(Location location) throws Exception {
        try{
        	em.persist(location);
        	em.flush();
        }catch (Exception e) {
        	throw new Exception(e.getMessage());
		}
    }

    public void update(Location location) {
        em.merge(location);
    }

    public void delete(Location location) {
        em.remove(em.contains(location) ? location : em.merge(location));
    }

}
